package com.gitee.wsl.mathematics.function.random

import kotlin.random.Random

class TableRandom(tableSize: Int = 1024 * 1024, generator: Random = Random) : Random() {
    private val random = IntArray(tableSize) { generator.nextInt() }
    private var index = 0

    override fun nextBits(bitCount: Int): Int {
        val r = random[index++]
        if (index == random.size) {
            index = 0
        }

        return if (bitCount == 32) r else r ushr (32 - bitCount)
    }
}